<template>
	<view class="container">
		<view class="yue">
			<view class="title">
				<image src="../../../static/icon/yue.png" mode=""></image>
				<text>余额</text>
			</view>
			<view class="jine">
				¥<text>{{info.user.money}}</text>
			</view>
			<navigator url="./chongzhi" hover-class="none">
				<view class="btn">
					充值
				</view>
			</navigator>
		</view>
		<view v-if="info.grade.length===0">
			<luanqing-empty :show="true" text="没有更多数据啦" textColor="#000"></luanqing-empty>
		</view>
		<view class="list" v-if="info.grade.length>0">
			<view class="listTitle">
				<view class="titleL">课程余额</view>
				<navigator url="./records" hover-class="none" class="titleR">消费记录</navigator>
			</view>
			<view class="content">
				
				<view class="item" v-for="(item,index) in info.grade" :key="index">
					<view class="pic">
						<image :src="item.thumb" mode=""></image>
					</view>
					<view class="intro">
						<view class="goodsName">
							{{item.goods_name}}
						</view>
						<view class="balance">
							<image src="../../../static/icon/yue01.png" mode=""></image>
							<!-- {{item.period_type===1?"剩余余额":"剩余课时"}}： -->
							剩余余额：
							<text>{{item.balance}}</text>
							<!-- <text v-if="item.period_type===2">课时</text> -->
						</view>
						<view class="time">
							<image src="../../../static/icon/shijian2.png" mode=""></image>
							<text>{{item.class_time}}</text>
						</view>
					</view>
					<view class="operate">
						<view class="xufei" @click="xufei(item.goods_id)">
							续费
						</view>
						<view class="tuikuan" @click="tuikuan(item)">
							退款
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="dialog">
		    <uni-popup-dialog mode="base" message="申请退款" 
			type="info"
			content="申请退款"
			:duration="2000" :before-close="true" @close="close" @confirm="confirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				info:"",
				token:"",
				goods_id:""
			}
		},
		created() {
			uni.showLoading({
				title:"加载中...",
				mask:true
			})
			uni.getStorage({
				key: 'user_info',
				success: (res) => {
					this.token = res.data.token;
					this.getData()
				}
			});
		},
		methods:{
			xufei(goods_id){
				uni.navigateTo({
					url: './xufei?goods_id='+goods_id
				});
			},
			getData() {
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/mine/index/myWallet`,
					data: {
						"token": this.token,
					},
					method: 'GET',
					success: res => {
						console.log(res)
						uni.hideLoading();
						if (res.data.code === 1) {
							this.info = res.data.data;
						} else if (res.data.code === 403) {
							uni.redirectTo({
								url: '../login/auth?redirect=../my/wallet/index'
							});
						} else {
							uni.showToast({
								title: '服务器开小差了呢，请您稍后再试',
								icon: 'none'
							})
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
					},
				});
			},
			confirm(){
				this.$refs.popup.close()
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/mine/index/refund`,
					data: {
						"token": this.token,
						"goods_id":this.goods_id
					},
					method: 'GET',
					success: res => {
						console.log(res)
						if (res.data.code === 1) {
							uni.showToast({
								title: '退款操作成功！',
								icon: 'none'
							})
						} else if (res.data.code === 403) {
							uni.redirectTo({
								url: '../login/auth'
							});
						} else {
							uni.showToast({
								title: '服务器开小差了呢，请您稍后再试',
								icon: 'none'
							})
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
					},
				});
			},
			tuikuan(obj){
				this.goods_id = obj.goods_id;
				this.$refs.popup.open("center");
			},
			close(){
				this.$refs.popup.close();
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100%;
		height: 100%;
		font-family: SourceHanSansCN-Medium, SourceHanSansCN;
		background: url(../../../static/img/topbg.png)no-repeat top left/100%;
		overflow: auto;
		margin-top: -2px;
	}
	.yue{
		width: 670rpx;
		height: 400rpx;
		background: #FFFFFF;
		box-shadow: 0px 2px 10px 0px #E9EBF6;
		border-radius: 8px;
		margin: 25px auto;
		overflow: hidden;
		.title{
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			margin-top: 54rpx;
			image{
				width: 27px;
				height: 27px;
			}
			text{
				font-size: 17px;
				font-weight: 500;
				color: #333333;
				margin-left: 9rpx;
			}
		}
	}
	.jine{
		font-size: 20px;
		font-weight: 500;
		color: #333333;
		text-align: center;
		margin-top: 26rpx;
		text{
			font-size: 36px;
		}
	}
	.btn{
		width: 320rpx;
		height: 68rpx;
		line-height: 68rpx;
		background: #4DD385;
		border-radius: 2px;
		font-size: 17px;
		color: #FFFFFF;
		text-align: center;
		margin: 55rpx auto 0 auto;
	}
	.list{
		padding: 0 40rpx;
		box-sizing: border-box;
	}
	.listTitle{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		.tieleL{
			font-size: 17px;
			font-weight: 500;
			color: #333333;
		}
		.titleR{
			font-size: 13px;
			color: #666666;
		}
	}
	.item{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
		.pic{
			width: 160rpx;
			height: 160rpx;
			border-radius: 8px;
			overflow: hidden;
			margin-right: 20rpx;
			image{
				width: 160rpx;
				height: 160rpx;
				border-radius: 8px;
			}
		}
		.intro{
			width: 400rpx;
			.grade_name{
				font-size: 15px;
				font-weight: 500;
				color: #333333;
			}
			.balance{
				font-size: 13px;
				color: #666666;
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-top: 10rpx;
				text{
					color: #DC3434;
				}
				image{
					width: 24rpx;
					height: 24rpx;
					margin-right: 10rpx;
				}
			}
			.time{
				font-size: 13px;
				color: #666666;
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-top: 10rpx;
				image{
					width: 26rpx;
					height: 24rpx;
					margin-right: 10rpx;
				}
			}
		}
		.operate{
			width: 110rpx;
			.xufei{
				width: 110rpx;
				height: 40rpx;
				line-height: 40rpx;
				background: #FFFFFF;
				border-radius: 2px;
				border: 1px solid #D11616;
				font-size: 13px;
				color: #D11616;
				text-align: center;
			}
			.tuikuan{
				font-size: 13px;
				color: #74A6ED;
				margin-top: 20rpx;
				text-align: center;
			}
		}
	}
</style>
